package edu.gatech.bsbmule.util;

import java.util.Scanner;

import edu.gatech.bsbmule.model.CustomizedTimer;
import edu.gatech.bsbmule.model.Game;
import edu.gatech.bsbmule.model.Map;
import edu.gatech.bsbmule.model.Store;

/**
 * Manages saving to JSON and loading from JSON
 * @author BSB
 *
 */
public class SaveAndLoadManager {
	
	/**
	 * Save the current game, map and store states to JSON
	 * 
	 * @return a String of JSON
	 */
	public static String save() {
		CustomizedTimer.getInstance().updateRemainingTime();
		String game = Game.saveToJson();
		String map = Map.saveToJson();
		String store = Store.saveToJson();
		String stats = StatisticsManager.saveToJson();
		return game + "\n" + map + "\n" + store + "\n" + stats;
	}
	
	/**
	 * Load a JSON file and construct game, map and store objects
	 * 
	 * @param json a string of JSON
	 */
	public static void load(String json) {
		Scanner scanner = new Scanner(json);
		String gameJson = scanner.nextLine();
		String mapJson = scanner.nextLine();
		String storeJson = scanner.nextLine();
		String statsJson = scanner.nextLine();
		scanner.close();
		Game.loadFromJson(gameJson);
		Map.loadFromJson(mapJson);
		Store.loadFromJson(storeJson);
		StatisticsManager.loadFromJson(statsJson);
		System.out.println("Resumed game");
	}
}
